<?php
require_once 'login_check.php';
check_admin_session();
session_start(); // 确保启动会话
require 'config.php'; // 引入数据库连接配置
// 初始化提示信息
$message = '';
$messageType = ''; // success/error

// 处理编辑请求
$editRecord = null;
if (isset($_GET['id'])) {
    try {
        $id = $_GET['id'];
        $sql = "SELECT * FROM time_duty WHERE id = :id";
        $stmt = $pdo->prepare($sql);
        $stmt->execute([':id' => $id]);
        $editRecord = $stmt->fetch(PDO::FETCH_ASSOC);
        if (!$editRecord) {
            throw new Exception('记录不存在');
        }
    } catch (Exception $e) {
        $message = '错误：' . addslashes($e->getMessage());
        $messageType = 'error';
    }
}
// 处理编辑提交
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_id'])) {
    try {
        $id = $_POST['edit_id'];
        $sql = "UPDATE time_duty SET 
            start_time = :start, 
            finish_time = :finish, 
            week = :week, 
            duty_cadre = :cadre, 
            duty_officer = :officer, 
            Duty_Officer_of_Squad_1 = :s1, 
            Duty_Officer_of_Squad_2 = :s2, 
            Duty_Officer_of_Squad_3 = :s3, 
            Duty_Officer_of_Squad_4 = :s4,
            date = :date
            WHERE id = :id";

        $stmt = $pdo->prepare($sql);
        $stmt->execute([
            ':start' => $_POST['start_time'],
            ':finish' => $_POST['finish_time'],
            ':week' => $_POST['week'],
            ':cadre' => $_POST['duty_cadre'],
            ':officer' => $_POST['duty_officer'],
            ':s1' => $_POST['squad1'],
            ':s2' => $_POST['squad2'],
            ':s3' => $_POST['squad3'],
            ':s4' => $_POST['squad4'],
            ':date' => $_POST['date'],
            ':id' => $id
        ]);
        // 存储成功消息并跳转
        $_SESSION['message'] = '记录已更新！';
        $_SESSION['messageType'] = 'success';
        header("Location: time_manage.php");
        exit;
    } catch (Exception $e) {
        // 存储错误消息并跳转
        $_SESSION['message'] = '更新失败：' . addslashes($e->getMessage());
        $_SESSION['messageType'] = 'error';
        header("Location: time_manage.php");
        exit;
    }
}

?>
<!DOCTYPE html>
<html>

<head>
    <title>值班信息管理</title>
    <meta charset="utf-8">
    <style>
        body {
            font-family: "Microsoft YaHei", Arial, sans-serif;
            margin: 20px;
        }

        .manage-container {
            max-width: 1000px;
            margin: 0 auto;
        }

        /* 提示信息样式 */
        .message {
            padding: 12px;
            margin: 10px 0;
            border-radius: 4px;
            font-weight: bold;
        }

        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        th,
        td {
            border: 1px solid #ddd;
            padding: 12px;
            text-align: left;
        }

        th {
            background-color: #f5f5f5;
            width: 120px;
        }

        .btn {
            padding: 8px 16px;
            background-color: #007bff;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 10px;
            text-decoration: none;
            display: inline-block;
        }

        .btn:hover {
            background-color: #0056b3;
        }

        .return-link {
            color: #007bff;
            text-decoration: none;
            display: inline-block;
            margin-top: 20px;
        }

        .horizontal-form tr {
            vertical-align: middle;
        }

        @media (max-width: 768px) {
            .horizontal-form td {
                display: block;
                width: 100%;
                padding: 8px 0;
            }

            .horizontal-form input,
            .horizontal-form select {
                width: 100%;
            }
        }
    </style>
</head>

<body>
    <div class="manage-container">
        <h2>值班信息管理</h2>
        <!-- 顶部返回按钮修改 -->
        <a href="time_manage.php" class="btn">返回</a>
        <hr>

        

        <?php if ($editRecord): ?>
            <h3>编辑周次 <?= $editRecord['week'] ?> 值班信息</h3>
            <form method="post" class="horizontal-form">
                <input type="hidden" name="edit_id" value="<?= $editRecord['id'] ?>">
                <table>
                    <tr>
                        <th>学期开始时间</th>
                        <td><input type="date" name="start_time" value="<?= $editRecord['start_time'] ?>" required></td>
                        <th>学期结束时间</th>
                        <td><input type="date" name="finish_time" value="<?= $editRecord['finish_time'] ?>" required></td>
                    </tr>
                    <tr>
                        <th>值班日期</th>
                        <td><input type="date" name="date" value="<?= $editRecord['date'] ?>" required></td>
                        <th>周次</th>
                        <td><input type="number" name="week" value="<?= $editRecord['week'] ?>" required></td>
                    </tr>
                    <tr>
                        <th>值班干部</th>
                        <td><input type="text" name="duty_cadre" value="<?= $editRecord['duty_cadre'] ?>" required></td>
                        <th>值班员</th>
                        <td><input type="text" name="duty_officer" value="<?= $editRecord['duty_officer'] ?>" required></td>
                    </tr>
                    <tr>
                        <th>一区队值班员</th>
                        <td><input type="text" name="squad1" value="<?= $editRecord['Duty_Officer_of_Squad_1'] ?>" required>
                        </td>
                        <th>二区队值班员</th>
                        <td><input type="text" name="squad2" value="<?= $editRecord['Duty_Officer_of_Squad_2'] ?>" required>
                        </td>
                    </tr>
                    <tr>
                        <th>三区队值班员</th>
                        <td><input type="text" name="squad3" value="<?= $editRecord['Duty_Officer_of_Squad_3'] ?>" required>
                        </td>
                        <th>四区队值班员</th>
                        <td><input type="text" name="squad4" value="<?= $editRecord['Duty_Officer_of_Squad_4'] ?>" >
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="text-align:center;">
                            <!-- 删除原"返回"按钮，仅保留保存按钮 -->
                            <button type="submit" class="btn">保存修改</button>
                        </td>
                    </tr>
                
        <?php endif; ?>
    </div>
</body>
</html>
